<template>
  <el-drawer size="80%" :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="项目名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入项目名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目版本" prop="version">
            <el-input v-model="formData.version" placeholder="请输入项目版本" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="项目描述" prop="description">
        <el-input
          v-model="formData.description"
          type="textarea"
          :rows="10"
          placeholder="请输入项目描述"
        />
      </el-form-item>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="负责人" prop="manager">
            <el-input v-model="formData.manager" placeholder="请输入负责人" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="存活状态" prop="status">
            <el-select v-model="formData.status" placeholder="请选择状态" style="width: 100%">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.AGENT_PROJECT_STATUS)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="进行状态" prop="progressStatus">
            <el-select v-model="formData.progressStatus" placeholder="请选择进行状态" style="width: 100%">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.AGENT_PROJECT_PROGRESS_STATUS)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目进度" prop="progress">
            <el-slider v-model="formData.progress" :max="100" show-input />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="开始日期" prop="startDate">
            <el-date-picker 
              v-model="formData.startDate" 
              type="date" 
              value-format="YYYY-MM-DD" 
              placeholder="请选择开始日期"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="截止日期" prop="deadline">
            <el-date-picker 
              v-model="formData.deadline" 
              type="date" 
              value-format="YYYY-MM-DD" 
              placeholder="请选择截止日期"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="代码地址" prop="codeUrl">
        <el-input v-model="formData.codeUrl" placeholder="https://github.com/..." />
      </el-form-item>

    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { ManageProjectApi, type ManageProjectVO } from '@/api/agent/manage/manageProject'
import { DICT_TYPE, getStrDictOptions } from '@/utils/dict'

/** 项目管理 表单 */
defineOptions({ name: 'ProjectForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  name: undefined,
  description: undefined,
  manager: undefined,
  version: 'v1.0.0',
  status: '',
  progressStatus: '',
  progress: 0,
  startDate: undefined,
  deadline: undefined,
  codeUrl: undefined,
  contentCount: 0,
  resourceCount: 0,
  meetingCount: 0,
  technicalCount: 0,
  documentCount: 0
})
const formRules = reactive({
  name: [{ required: true, message: '项目名称不能为空', trigger: 'blur' }],
//   description: [{ required: true, message: '项目描述不能为空', trigger: 'blur' }],
//   manager: [{ required: true, message: '负责人不能为空', trigger: 'blur' }],
//   status: [{ required: true, message: '存活状态不能为空', trigger: 'change' }],
//   progressStatus: [{ required: true, message: '进行状态不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ManageProjectApi.getManageProject(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ManageProjectVO
    if (formType.value === 'create') {
      await ManageProjectApi.createManageProject(data)
      message.success(t('common.createSuccess'))
    } else {
      await ManageProjectApi.updateManageProject(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    description: undefined,
    manager: undefined,
    version: 'v1.0.0',
    status: '',
    progressStatus: '',
    progress: 0,
    startDate: undefined,
    deadline: undefined,
    codeUrl: undefined,
    contentCount: 0,
    resourceCount: 0,
    meetingCount: 0,
    technicalCount: 0,
    documentCount: 0
  }
  formRef.value?.resetFields()
}
</script>